package com.eggplant.timetools.db;

import java.util.HashMap;

import com.eggplant.timetools.bean.TaskItem;
import com.eggplant.timetools.bean.TaskLogsItem;
import com.eggplant.timetools.utils.MyLog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;

public class TaskItemDb extends SQLiteOpenHelper {
	public static final String ERR_TAG = "TaskItemDb.java";
	private static final String DATABASE_NAME = "TaskItemDb.db";
	private static final int DATABASE_VERSION = 2;
	private static final String[] TASK_PROJECT =new String[]{
		"target","coreNum","timeLeng","type","buildTime","nextTime","detail","timeInte","noticeWays","repeatStrage","coreDes"
	};
	private static final String[] LOG_PROJECT =new String[]{
		"target","coreNum","timeLeng","buildTime","runTime","isOK"
	};

	private static TaskItemDb mInstance = null;
	HashMap<Long, TaskItem> map = new HashMap<Long, TaskItem>();

	public static synchronized TaskItemDb getInstance(Context context) {
		if (null == mInstance) {
			mInstance = new TaskItemDb(context.getApplicationContext());
		}
		return mInstance;
	}

	public static synchronized void freeInstance() {
		mInstance = null;
	}

	Context mContext;

	private TaskItemDb(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE tasks(_id INTEGER PRIMARY KEY,target TEXT,type INTEGER,buildTime LONG,nextTime LONG default -1,detail TEXT," +
				"coreNum INTEGER,timeLeng INTEGER,timeInte INTEGER,noticeWays INTEGER,repeatStrage INTEGER,coreDes TEXT);");
		db.execSQL("CREATE TABLE tasklogs(_id INTEGER PRIMARY KEY,target TEXT,buildTime LONG,runTime LONG,isOk BOOLEAN,coreNum INTEGER,timeLeng INTEGER);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {		
	}

	public synchronized HashMap<Long, TaskItem> getTaskList(boolean isRefresh) {
		if(map.size()>0&&!isRefresh)
			return map;
		MyLog.i("ydy","*******getTaskList");
		map.clear();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables("tasks");
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = qb.query(db,TASK_PROJECT, null, null, null,
				null, null);

		if (c != null && c.getCount() > 0) {
			while (c.moveToNext()) {
				
			TaskItem item = new TaskItem();
			item.target=c.getString(0);
			item.coreNum=c.getInt(1);
			item.timeLeng=c.getInt(2);
			item.type=c.getInt(3);
			item.buildTime=c.getLong(4);
			item.nextTime=c.getLong(5);
			item.detail=c.getString(6);
			item.timeInte=c.getInt(7);
			item.noticeWays=c.getInt(8);
			item.repeatStrage=c.getInt(9);
			item.coreDes=c.getString(10);
			MyLog.i("ydy", "target=="+item.target+" coreNum=="+item.coreNum+" timeLeng=="+item.timeLeng+" type"+
					item.type+" buildTime=="+item.buildTime+" nextTime="+item.nextTime+" detail=="+item.detail+" ways=="+item.noticeWays);
			map.put(item.buildTime, item);
			}
		}
		if (c != null)
			c.close();
		return map;
	}
	
	
	public synchronized TaskItem qureyTaskByKey(String key) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables("tasks");
		qb.appendWhere("buildTime="+key);
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = qb.query(db,TASK_PROJECT, null, null, null,
				null, null);
		TaskItem item=null;
		if (c != null && c.getCount() > 0) {
			c.moveToFirst();
						
			 item = new TaskItem();
			item.target=c.getString(0);
			item.coreNum=c.getInt(1);
			item.timeLeng=c.getInt(2);
			item.type=c.getInt(3);
			item.buildTime=c.getLong(4);
			item.nextTime=c.getLong(5);
			item.detail=c.getString(6);
			item.timeInte=c.getInt(7);
			item.noticeWays=c.getInt(8);
			item.repeatStrage=c.getInt(9);
			item.coreDes=c.getString(10);
			MyLog.i("ydy", "target=="+item.target+" coreNum=="+item.coreNum+" timeLeng=="+item.timeLeng+" type"+
					item.type+" buildTime=="+item.buildTime+" nextTime="+item.nextTime+" detail=="+item.detail+" ways=="+item.noticeWays);
		
			
			
		}
		if (c != null)
			c.close();
		return item;
	}
	
	
	public synchronized HashMap<Long, TaskLogsItem> getLogsList(Long buildTime) {
		HashMap<Long, TaskLogsItem> map = new HashMap<Long, TaskLogsItem>();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables("tasklogs");
		qb.appendWhere("buildTime="+buildTime);
		SQLiteDatabase db = getWritableDatabase();
		Cursor c=db.rawQuery("select target,coreNum,timeLeng,buildTime,runTime,isOK from tasklogs where buildTime=? order by runTime desc limit 50", 
				new String[]{buildTime+""});
		
	/*	Cursor c = qb.query(db, LOG_PROJECT, "limit 1", null, null,
				null, "runTime desc");*/

		if (c != null && c.getCount() > 0) {
			while (c.moveToNext()) {
				TaskLogsItem item = new TaskLogsItem();
				item.target=c.getString(0);
				item.coreNum=c.getInt(1);
				item.timeLeng=c.getInt(2);
				item.buildTime=c.getLong(3);
				item.runTime=c.getLong(4);
				item.isOk=c.getInt(5)>0;
				map.put(item.runTime, item);
			}
		}
		if (c != null)
			c.close();
		return map;
	}

	public boolean insertTask(final TaskItem item) {
		SQLiteDatabase db = getWritableDatabase();
		MyLog.i("ydy", "TaskItemDb insertTask item=="+item.target+" "+item.coreNum);
		db.execSQL("delete from tasks where buildTime='" + item.buildTime + "'");
		ContentValues cv = new ContentValues();
		cv.put("type",item.type);
		cv.put("buildTime", item.buildTime);
		cv.put("nextTime", item.nextTime);
		cv.put("target",item.target);
		cv.put("detail", item.detail);
		cv.put("coreNum",item.coreNum);
		cv.put("timeLeng", item.timeLeng);
		cv.put("timeInte", item.timeInte);
		cv.put("noticeWays", item.noticeWays);
		cv.put("repeatStrage",item.repeatStrage);
		cv.put("coreDes",item.coreDes);
		long result =db.insert("tasks", null, cv);
		if(result!=-1)
			return true;
		else
			return false;
	}

	public void deleteTask(final long buildTime) {
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL("delete from tasks where buildTime='" + buildTime + "'");
	}

	public boolean insertTaskLog(final TaskLogsItem item) {
		SQLiteDatabase db = getWritableDatabase();
		if(item.runTime!=-1)
		db.execSQL("delete from tasklogs where runTime='" + item.runTime + "'");
		ContentValues cv = new ContentValues();
		cv.put("runTime",item.runTime);
		cv.put("buildTime", item.buildTime);
		cv.put("isOk", item.isOk);
		cv.put("target",item.target);
		cv.put("timeLeng",item.timeLeng);
		cv.put("coreNum",item.coreNum);
		long result =db.insert("tasklogs", null, cv);
		if(result!=-1)
			return true;
		else
			return false;
	}

	public void deleteTaskLog(final String buildTime ) {
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL("delete from tasklogs where buildTime='" + buildTime + "'");
	}
}
